import React from 'react';
import {inject, observer} from 'mobx-react';
import {Button, Col, message, Modal, Spin, Tabs, Tooltip} from 'antd';
import {
    WeaBrowser,
    WeaDatePicker,
    WeaDialog,
    WeaInput,
    WeaLeftRightLayout,
    WeaLocaleProvider,
    WeaNewScroll,
    WeaPopoverHrm,
    WeaReqTop,
    WeaRichText,
    WeaRightMenu,
    WeaSearchGroup,
    WeaTab,
    WeaTag,
    WeaCheckbox
} from 'ecCom';
import LoadLogs from '../../../public/loadlog/loadLogs'
import WrShow from '../../../public/show'
import ProgramBrowser from './ProgramBrowser'
import ProgramItemBrowser from './ProgramItemBrowser'
import {
    getCurrentHeight,
    getParams,
    renderAlertPage,
    setWindowKeys,
    transfStr,
    idsAndNamesToArr
} from '../../../util/Util'
import PersonTree from '../../../public/persontree/PersonTree'
import ProgramTarget from "./ProgramTarget";
import ProgramItemList from "../programItem/ProgramItemList";
import ProgramScore from "./ProgramScore";
import ProgramScoreDetail from './ProgramScoreDetail';
import {WeaTableNew} from "comsMobx";

const WeaTable = WeaTableNew.WeaTable;
const toJS = mobx.toJS;
const getLabel = WeaLocaleProvider.getLabel;
const TabPane = Tabs.TabPane;

/**
 * 绩效-考核方案
 * jiansong 2019/6/17
 */
@inject("programViewStore", "programItemStore")
@observer
export default class ProgramView extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            showLeft: false, //是否显示左侧栏
            hrmname: '',//人员名称
            _height: this.props.height == undefined ? getCurrentHeight() : this.props.height,
            showItemList: false,//是否展示考核模板列表
        };
        this.store = this.props.programViewStore;

        //是否考核组设置
        if (this.props.backSetting != undefined) {
            this.store.backSetting = this.props.backSetting;
        }
        //是否考核组新增
        if (this.store.backSetting || this.props.isMy) {
            this.store.form.resourceid = null;
            this.store.form.programid = null;
        }
    }

    componentWillMount() {
        setWindowKeys(3, getLabel(506163, "我的考核方案"));
        //跳转过来的页面，获取参数
        if (getParams("programid")) {
            this.store.form.programid = getParams("programid");
        }
        if (getParams("resourceid")) {
            this.store.form.resourceid = getParams("resourceid");
            this.store.form.programtype = getParams("programtype");
        }
        if (this.props.groupid) {
            this.store.form.groupid = this.props.groupid;
        }
        this.store.doInit();
    }

    render() {
        let {backSetting, programStatus, showDialog, showItemDialog, backProgram, showNoticeMessage, colorType, showStatus, loading, hasRight, changeProgramId, msg, hasCycle, form, detail, logList, otherProgram, logLastCount, auditidsData, programDetail, onFormChange, addStatus, a_remark} = this.store;
        let {_height, hrmname, showItemList} = this.state;

        const collectParams = {
            favname: detail.titlename || getLabel('509541', '{name}绩效考核方案设置').replace('{name}', hrmname),
            favouritetype: "5",
            link: '/spa/workrelate/static/index.html#/main/workrelate/performance/programView?programid=' + form.programid + "&groupid=" + form.groupid + "&resourceid=" + form.resourceid + "&backSetting=false",
            importantlevel: 1,
        }
        return (
            <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@yhq5ll`} spinning={loading}>
                <div className="wea-workrelate-performance-programView">
                    <div style={{height: backSetting ? _height : '100vh'}}>
                        <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@yvp129`} datas={this.getMenu()} collectParams={collectParams} showUrlItem={false}>
                            {showItemList && <ProgramItemList ecId={`${this && this.props && this.props.ecId || ''}_ProgramItemList@ufnevn`}
                                show={showItemList}
                                showStatus={showStatus}
                                changeShow={(val) => this.setState({showItemList: val})}
                                onQuoteNumClick={val => {
                                    this.store.showItemDialog = true;
                                    this.store.quoteParams.templateid = val.id;
                                    this.store.quoteParams.type = "item";
                                    getQuoteListData();
                                }}
                            />}
                            {showDialog && <ProgramBrowser ecId={`${this && this.props && this.props.ecId || ''}_ProgramBrowser@gdda8l`} updateNum={() => this.programBrowserUpdateNum()}/>}
                            {showItemDialog && <ProgramItemBrowser ecId={`${this && this.props && this.props.ecId || ''}_ProgramItemBrowser@bj0ead`}/>}
                            <WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@cszqch`}/>
                            {!backSetting ? <WeaReqTop ecId={`${this && this.props && this.props.ecId || ''}_WeaReqTop@b8ojzn`}
                                title={detail.titlename || getLabel('509541', '{name}绩效考核方案设置').replace('{name}', this.state.hrmname)}
                                icon={<i className='icon-coms-implement'/>}
                                iconBgcolor='rgb(150, 53, 138)'
                                loading={loading}
                                buttons={this.getButton(this.props.ecId || '')}
                                showDropIcon={true}
                                dropMenuDatas={this.getMenu()}
                                tabDatas={detail.stabList}
                                keyParam="viewcondition"
                                selectedKey={form.programtype}
                                onChange={(val) => {
                                    onFormChange({"programtype": val, "programid": "", "groupid": ""});
                                    programDetail();
                                }}
                            >
                                <WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@bw7jjz`}
                                    isNew={true}
                                    showLeft={true}
                                    leftWidth={220}
                                    leftCom={
                                        <PersonTree ecId={`${this && this.props && this.props.ecId || ''}_PersonTree@jne4wz`}
                                            onPersonClick={this.onPersonClick}
                                            treeNodeClick={this.treeNodeClick}
                                        />}
                                    showLeft={this.state.showLeft}
                                    onCollapse={showLeft => this.setState({showLeft})}
                                >
                                    {this.getWrShow(backProgram, msg, hasCycle, hasRight, loading, form, showStatus, backSetting, _height, onFormChange, addStatus, colorType, detail, programStatus, a_remark, auditidsData, logList, logLastCount, otherProgram, changeProgramId)}
                                </WeaLeftRightLayout>
                            </WeaReqTop> : <div>
                                <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@3rfxt6`}
                                    datas={detail.stabList}
                                    keyParam="viewcondition"
                                    selectedKey={form.programtype}
                                    onChange={(val) => {
                                        onFormChange({"programtype": val, "programid": ""});
                                        programDetail();
                                    }}
                                />
                                {this.getWrShow(backProgram, msg, hasCycle, hasRight, loading, form, showStatus, backSetting, _height, onFormChange, addStatus, colorType, detail, programStatus, a_remark, auditidsData, logList, logLastCount, otherProgram, changeProgramId)}
                            </div>
                            }
                        </WeaRightMenu>
                    </div>
                </div>
            </Spin>
        )
    }

    getWrShow(backProgram, msg, hasCycle, hasRight, loading, form, showStatus, backSetting, _height, onFormChange, addStatus, colorType, detail, programStatus, a_remark, auditidsData, logList, logLastCount, otherProgram, changeProgramId) {
        return <WrShow ecId={`${this && this.props && this.props.ecId || ''}_WrShow@t4w1n3`} show={msg == '1'}>
            <div>
                {!hasCycle && renderAlertPage('<span style="color:red">' + getLabel('508557', '暂未开启任何考核周期') + '</span>','','','',this.props.ecId || '')}
                {!hasRight && renderAlertPage('<span style="color:red">' + getLabel('508609', '您暂时无权限查看此数据') + '</span>','','','',this.props.ecId || '')}
                <div style={{display: (hasCycle && hasRight) ? 'block' : 'none'}}>
                    <div className={'no-program-span'}
                         style={{display: (!loading && form.programid == '' && !showStatus) ? 'block' : 'none'}}>{getLabel(509520, '暂未设置相应考核方案！')}</div>
                    <div
                        style={{display: (form.programid == '' && !showStatus) ? 'none' : 'block'}}>
                        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@r405n7`}
                            height={backSetting ? _height - 48 : _height - 110}>
                            {this.buildHeader(backProgram, backSetting, showStatus, form, onFormChange, addStatus, colorType, detail, programStatus)}
                            <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@n7lm69`}
                                title={getLabel('386205', '考核明细')}
                                showGroup={true}>
                                <ProgramTarget ecId={`${this && this.props && this.props.ecId || ''}_ProgramTarget@qcrzf7`}
                                    detailList={form.assessDetailData}
                                    checkDisabledTarget={true}
                                    onDataChange={val => onFormChange({assessDetailData: val})}
                                    accessItemList={detail.accessItemList}
                                    showStatus={showStatus}
                                    showTitle={false}
                                />
                            </WeaSearchGroup>
                            <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@eot2t5`}
                                title={getLabel('15752', '考核说明')}
                                showGroup={true}>
                                {this.buildRichText(showStatus, a_remark, onFormChange, form)}
                            </WeaSearchGroup>
                            <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@b0mivv`}
                                title={getLabel('15489', '考核评分')}
                                showGroup={true}>
                                <ProgramScoreDetail ecId={`${this && this.props && this.props.ecId || ''}_ProgramScoreDetail@ejjers`}
                                    detailList={form.accessScoreData}
                                    onDataChange={val => onFormChange({"accessScoreData": val})}
                                    showStatus={showStatus}
                                    managerid={detail.managerid}
                                    managername={detail.managername}
                                    isFromList={this.state.showItemList}
                                    backSetting={backSetting}
                                />
                            </WeaSearchGroup>
                            <div className='date-div'>
                                <span>{getLabel('508675', '考核审批')}：</span>
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@g88ro9`} span={10} style={{lineHeight: '30px'}}>
                                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@s7eb32`}
                                        type={17}
                                        isSingle={false}
                                        title={getLabel('24743', '审批人')}
                                        viewAttr={showStatus ? 2 : 1}
                                        replaceDatas={auditidsData}
                                        onChange={(ids, names) => {
                                            onFormChange({
                                                "auditids": ids,
                                                "auditnames": names
                                            })
                                        }}
                                    />
                                </Col>
                                {!backSetting && showStatus &&
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@n7lqlc`} span={4} offset={1}>
                                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@dm0odh`}
                                        onClick={this.getManagerManager}>{getLabel('508574', '上级的上级')}</Button>
                                </Col>}
                                {backSetting &&
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@dtoygi`} span={4} offset={1}>
                                    <span style={{marginRight: 10}}>{getLabel('508574', '上级的上级')}</span>
                                    <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@242hms`}
                                        value={this.store.auditidsManager}
                                        disabled={!showStatus}
                                        display="switch"
                                        style={{display: 'inline'}}
                                        onChange={(val) => this.store.auditidsManager = (val == 1 ? true : false)}
                                    />
                                </Col>}
                            </div>
                            <WrShow ecId={`${this && this.props && this.props.ecId || ''}_WrShow@brwu69`}
                                show={(!backProgram || (backSetting && backProgram)) && !addStatus && form.programid != undefined && form.programid != ''}>
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@xg958h`} span={12}>
                                    <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@j4xjm8`}
                                        title={getLabel('32061', '操作日志')}
                                        showGroup={true}>
                                        <LoadLogs ecId={`${this && this.props && this.props.ecId || ''}_LoadLogs@vm79e2`}
                                            logsData={toJS(logList)}
                                            lastcount={logLastCount}
                                            onMoreClick={() => this.onMoreClick()}/>
                                    </WeaSearchGroup>
                                </Col>
                            </WrShow>
                            <WrShow ecId={`${this && this.props && this.props.ecId || ''}_WrShow@6ycwj6`}
                                show={!addStatus && otherProgram && otherProgram.length > 0}>
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@y9wnrx`} span={12}>
                                    <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@13m8nm`}
                                        title={getLabel('508677', '其他考核方案')}
                                        showGroup={true}>
                                        {!addStatus && otherProgram && otherProgram.length > 0 && this.buildOtherProgram(otherProgram, changeProgramId, programStatus)}
                                    </WeaSearchGroup>
                                </Col>
                            </WrShow>
                        </WeaNewScroll>
                    </div>
                </div>
            </div>
        </WrShow>;
    }

    //构建方案头部信息
    buildHeader(backProgram, backSetting, showStatus, form, onFormChange, addStatus, colorType, detail, programStatus) {
        return <div>
            <div className='date-div'>
                <div style={{display: 'inline-block'}}>
                    <span>{getLabel('19548', '生效日期')}：</span>
                    {showStatus ? <WeaDatePicker ecId={`${this && this.props && this.props.ecId || ''}_WeaDatePicker@icp7nf`}
                            viewAttr={3}
                            value={form.startdate}
                            onChange={val => {
                                onFormChange({"startdate": val})
                                this.store.isChangeStartdate = true
                            }}
                            style={{marginRight: 20}}
                        /> :
                        <span className='date-span'> {form.startdate} </span>}
                </div>
                {!backSetting && backProgram && !addStatus &&
                <WeaTag ecId={`${this && this.props && this.props.ecId || ''}_WeaTag@39hotf`} color={"red"}
                        style={{color: 'white', borderRadius: '4px'}}>
                    {getLabel('518208', '考核组方案')}
                </WeaTag>}
                {this.buildAtditPerson(addStatus, colorType, detail, programStatus)}
                {!backProgram && detail.noprogram && detail.nomsg &&
                <span className='no-finish-program'>{detail.nomsg}</span>}
                {this.store.showNoticeMessage &&
                <span style={{"padding-left": '10px', color: "red"}}>{detail.noticeMessage}</span>}
            </div>
        </div>
    }

    //构建其他考核方案
    buildOtherProgram(otherProgram, changeProgramId, programStatus) {
        return <div className="other-program-view">
            {
                otherProgram.map(item => <div className="other-program-div">
                    <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@k5e9xc@${item.id}`}
                        title={this.getOtherProgramTooltip(item)}>
                        <a onClick={() => {
                            changeProgramId(item.id, item.groupid)
                        }}>{item.programname + "（" + programStatus[item.programstatus] + "）" + (item.groupid ? getLabel('518208', '考核组方案') : '')}</a>
                    </Tooltip>
                </div>)
            }
        </div>;
    }

    //构建富文本
    buildRichText(showStatus, a_remark, onFormChange, form) {
        return <div>
            <WrShow ecId={`${this && this.props && this.props.ecId || ''}_WrShow@0uxyxv`} show={showStatus}>
                <WeaRichText ecId={`${this && this.props && this.props.ecId || ''}_WeaRichText@xpgn3b`}
                    ckConfig={{
                        toolbar: [
                            {name: 'document', items: ['Source']},
                            {name: 'clipboard', items: ['Undo', 'Redo']},
                            {
                                name: 'paragraph',
                                items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', '-', 'NumberedList', 'BulletedList']
                            },
                            {
                                name: 'styles',
                                items: ['Format', 'Font', 'FontSize']
                            },
                            {name: 'colors', items: ['TextColor']},
                            {
                                name: 'basicstyles',
                                items: ['Bold', 'Italic', 'Underline', 'Strike',]
                            },
                            {
                                name: 'insert',
                                items: ['Image', 'Flash', 'Table', 'Smiley']
                            },
                            {name: 'tools', items: ['Maximize']},
                        ],
                        startupFocus: false,
                        height: 200,
                        autoGrow_minHeight: 200,
                        autoGrow_maxHeight: 200,
                        uploadUrl: '/api/doc/upload/uploadFile',
                        resize_enabled: false,
                    }}
                    value={form.remark}
                    onToolsChange={(name, ids, list, type) => transfStr(name, ids, list, type)}
                    onChange={(val) => onFormChange({"remark": val})}
                />
            </WrShow>
            <WrShow ecId={`${this && this.props && this.props.ecId || ''}_WrShow@eceic2`} show={!showStatus}>
                <div className="program-remark wea-workrelate-remark-number"
                     dangerouslySetInnerHTML={{__html: form.remark}}/>
            </WrShow>
        </div>
    }

    //构建审批人
    buildAtditPerson(addStatus, colorType, detail, programStatus) {
        return <span className='atdit-person'>
            <WrShow ecId={`${this && this.props && this.props.ecId || ''}_WrShow@myy1kx`} show={!addStatus}>
              <WeaTag ecId={`${this && this.props && this.props.ecId || ''}_WeaTag@18absk`} color={colorType[detail.programstatus]}
                      style={{color: 'white', borderRadius: '4px'}}>
                  {programStatus[detail.programstatus]}
              </WeaTag>
            </WrShow>
            <WrShow ecId={`${this && this.props && this.props.ecId || ''}_WrShow@fepcew`} show={!addStatus && detail.programstatus == '1'}>
                <span style={{'margin-left': '10px'}}>
                    {getLabel('129864', '待审批人')}：<span
                    dangerouslySetInnerHTML={{__html: detail.waitAuditName}}/>
                </span>
            </WrShow>
        </span>;
    }

    //获取其他考核方案的Tooltip
    getOtherProgramTooltip(item) {
        let {programStatus} = this.store;
        let message = item.programname + "(" + getLabel('23247', '状态') + ':' + programStatus[item.programstatus];
        if (item.createtime) {
            message += " " + getLabel('30436', '创建时间') + ":" + item.createtime
        }
        message += ")";
        return message;
    }

    //点击节点
    treeNodeClick = (event, ids, selectedKeys, nodes) => {
        const {onFormChange, programDetail} = this.store;
        let {name, type, id} = nodes[0];
        switch (type) {
            case '3':
                onFormChange({'resourceid': id, 'programid': '', 'programtype': null, "groupid": ''});
                programDetail();
                this.setState({
                    hrmname: name
                })
                break;
        }
    }

    //点击人员
    onPersonClick = (hrmId, hrmname) => {
        const {onFormChange, programDetail} = this.store;
        onFormChange({'resourceid': hrmId, 'programid': '', 'programtype': null, "groupid": ''});
        programDetail();
        this.setState({
            hrmname: hrmname
        })
    }

    //日志加载更多
    onMoreClick() {
        this.store.logCurrent = this.store.logCurrent + 1;
        this.store.getLogList();
    }

    //获取操作菜单
    getMenu = () => {
        const {backProgram, backSetting, showStatus, canAdd, addStatus, operation, initQuoteDialog, doAdd, cancelAdd, detail: {canResetScore, canedit, candelete, canquoteindex, programstatus, canaudit}} = this.store
        let menu = [];
        if (!addStatus && backProgram) {
            if (canAdd) {
                menu.push({
                    key: '0',
                    icon: <i className='icon-coms-Add-to'/>,
                    content: getLabel('365', '新建'),
                    onClick: doAdd,
                });
            }
            if (!backSetting && canResetScore) {
                menu.push({
                    key: '12',
                    icon: <i className="icon-coms-Restore"/>,
                    content: getLabel('516940', '重置评分'),
                    onClick: () => {
                        Modal.confirm({
                            title: getLabel('386786', '信息确认'),
                            content: getLabel('516953', '重置评分会影响进行中的评分数据，确定要重置？'),
                            onOk: () => {
                                operation("resetScore", this.props.updateNum);
                            },
                        });
                    }
                });
            }
            return menu;
        }
        if (canAdd && !addStatus) {
            menu.push({
                key: '0',
                icon: <i className='icon-coms-Add-to'/>,
                content: getLabel('365', '新建'),
                onClick: doAdd,
            });
        }
        if (showStatus) {
            menu.push({
                key: '11',
                icon: <i className="icon-coms-task-list"/>,
                content: "模板列表",
                onClick: () => {
                    this.props.programItemStore.getProgramItemList();
                    this.setState({showItemList: true})
                }
            });
        }
        if (!backSetting && !addStatus && canResetScore) {
            //有重置评分的权限的人才能操作
            menu.push({
                key: '12',
                icon: <i className="icon-coms-Restore"/>,
                content: getLabel('516940', '重置评分'),
                onClick: () => {
                    Modal.confirm({
                        title: getLabel('386786', '信息确认'),
                        content: getLabel('516953', '重置评分会影响进行中的评分数据，确定要重置？'),
                        onOk: () => {
                            operation("resetScore", this.props.updateNum);
                        },
                    });
                }
            });
        }
        if (!backSetting && canquoteindex && !addStatus) {
            menu.push({
                key: '1',
                icon: <i className='icon-coms-copy'/>,
                content: getLabel('508693', '引用指标'),
                onClick: () => initQuoteDialog(false),
            });
        }
        if (!backSetting && (canedit || canAdd) && programstatus!=1) {
            menu.push({
                key: '2',
                icon: <i className='icon-coms-copy'/>,
                content: getLabel('508694', '引用方案'),
                onClick: () => initQuoteDialog(true),
            });
            if ("" != programstatus && "13" == programstatus && !addStatus) {
                menu.push({
                    key: '3',
                    icon: <i className='icon-coms-Reopen'/>,
                    content: getLabel('26472', '启用'),
                    onClick: () => this.operationForm("start"),
                });
            } else if ("" != programstatus && "0" != programstatus && !addStatus) {
                menu.push({
                    key: '4',
                    icon: <i className='icon-coms-Return'/>,
                    content: getLabel('18096', '禁用'),
                    onClick: () => this.operationForm("forbidden"),
                });
            }
            menu.push({
                key: '5',
                icon: <i className='icon-coms-Save-as'/>,
                content: getLabel('30986', '保存'),
                onClick: () => this.operationForm("save"),
            });
            menu.push({
                key: '6',
                icon: <i className='icon-coms-top'/>,
                content: getLabel('725', '提交'),
                onClick: () => this.operationForm("submit"),
            });
        }
        if (!backSetting && candelete) {
            menu.push({
                key: '10',
                icon: <i className='icon-coms-delete'/>,
                content: getLabel(91, '删除'),
                onClick: () => this.doDelete("delete"),
            });
        }
        if (canaudit) {
            menu.push({
                key: '7',
                icon: <i className='icon-coms-export'/>,
                content: getLabel('142', '批准'),
                onClick: () => this.programAudit("approve"),
            });
            menu.push({
                key: '8',
                icon: <i className="icon-coms-go-back"/>,
                content: getLabel('236', '退回'),
                onClick: () => this.programAudit("return"),
            });
        }
        if (addStatus) {
            menu.push({
                key: '9',
                icon: <i className='icon-coms-export'/>,
                content: getLabel('31129', '取消'),
                onClick: cancelAdd,
            });
        }
        return menu;
    }

    //获取操作按钮
    getButton = (ecId) => {
        const {backProgram, canAdd, addStatus, doAdd, cancelAdd, initQuoteDialog, detail: {canedit, candelete, canquoteindex, programstatus, canaudit}} = this.store
        let button = [];
        if (!addStatus && backProgram) {
            if (canAdd) {
                button.push(
                    <Tooltip ecId={`${ecId}_Tooltip@fjld63@create`} title={getLabel('508716', '建立新考核方案')}>
                        <Button ecId={`${ecId}_Button@vjt7vm@create`} type='primary' onClick={doAdd}>
                            {getLabel('365', '新建')}
                        </Button>
                    </Tooltip>
                );
            }
            return button;
        }
        //允许新建方案并且目前状态时未新建状态
        if (canAdd && !addStatus) {
            button.push(
                <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@wcmqom@create`} title={getLabel('508716', '建立新考核方案')}>
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@8skshv@create`} type='primary' onClick={doAdd}>
                        {getLabel('365', '新建')}
                    </Button>
                </Tooltip>
            );
        }
        //允许引用指标方案并且目前状态时未新建状态
        if (canquoteindex && !addStatus) {
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@mz4iob@quoteindex`} title={getLabel('508717', '引用其他方案指标')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@06lr2v@quoteindex`} type='primary' onClick={() => initQuoteDialog(false)}>
                    {getLabel('508693', '引用指标')}
                </Button>
            </Tooltip>);
        }
        if (canedit && !addStatus) {
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@5i5bhr@quoteprogram`} title={getLabel('508718', '引用其他方案')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9svx1s@quoteprogram`} type='primary' onClick={() => initQuoteDialog(true)}>
                    {getLabel('508694', '引用方案')}
                </Button>
            </Tooltip>);
            if ("" != programstatus && "13" == programstatus && !addStatus) {
                button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@nkaccr@enable`} title={getLabel('33164', '启用方案')}>
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ifr5pf@enable`} type='primary' onClick={() => this.operationForm("start")}>
                        {getLabel('26472', '启用')}
                    </Button>
                </Tooltip>);
            } else if ("" != programstatus && "0" != programstatus && !addStatus) {
                button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@3t932r@disable`} title={getLabel('508719', '禁用方案')}>
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@66w4c7@disable`} type='primary' onClick={() => this.operationForm("forbidden")}>
                        {getLabel('18096', '禁用')}
                    </Button>
                </Tooltip>);
            }
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@lyyla9@save`} title={getLabel('508724', '保存为草稿')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@t755d0@save`} type='primary' onClick={() => this.operationForm("save")}>
                    {getLabel('30986', '保存')}
                </Button>
            </Tooltip>);
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@n8gurz@submit`} title={getLabel('382335', '保存并提交')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9rn0po@submit`} type='primary' onClick={() => this.operationForm("submit")}>
                    {getLabel('725', '提交')}
                </Button>
            </Tooltip>);
        }
        if (candelete) {//删除
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@usmkyo@delete`} title={getLabel(91, '删除')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@dm9fdp@delete`} type='primary' onClick={() => this.doDelete("delete")}>
                    {getLabel(91, '删除')}
                </Button>
            </Tooltip>);
        }
        if (canaudit) {
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@1bxrrr@approve`} title={getLabel('508725', '批准考核方案')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@biv29t@approve`} type='primary' onClick={() => this.programAudit("approve")}>
                    {getLabel('142', '批准')}
                </Button>
            </Tooltip>);
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@p7mepo@return`} title={getLabel('508726', '退回考核方案')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@enti4e@return`} type='primary' onClick={() => this.programAudit("return")}>
                    {getLabel('236', '退回')}
                </Button>
            </Tooltip>);
        }
        if (addStatus) {
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@x3w92j@quoteprogram`} title={getLabel('508718', '引用其他方案')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@dqeii4@quoteprogram`} type='primary' onClick={() => initQuoteDialog(true)}>
                    {getLabel('508694', '引用方案')}
                </Button>
            </Tooltip>);
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@f0hmqm@save`} title={getLabel('508724', '保存为草稿')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@unnjfw@save`} type='primary' onClick={() => this.operationForm("save")}>
                    {getLabel('30986', '保存')}
                </Button>
            </Tooltip>);
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@shvlq8@submit`} title={getLabel('382335', '保存并提交')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@jlccej@submit`} type='primary' onClick={() => this.operationForm("submit")}>
                    {getLabel('725', '提交')}
                </Button>
            </Tooltip>);
            button.push(<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@yzegnv@cancel`} title={getLabel('31129', '取消')}>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@mmkv9h@cancel`} type='primary' onClick={cancelAdd}>
                    {getLabel('31129', '取消')}
                </Button>
            </Tooltip>);
        }
        return button;
    }

    //校验表单
    operationForm = (type) => {
        const {operation, form: {startdate, assessDetailData, accessScoreData}, detail: {accessItemList}} = this.store;
        if (startdate == undefined || startdate == '') {
            message.warning(getLabel('509122', '请选择生效日期！'));
            return false;
        }
        let flag = accessScoreData.every(item => item.templateid);
        if (!flag && assessDetailData.length == 0) {
            message.warning(getLabel('517028', '请添加考核指标或给每个评分人设置考核模板'));
            return false;
        }
        if (assessDetailData.length > 0) {
            if (assessDetailData.some(item => item.accessitemid == undefined || item.accessitemid == '')) {
                message.warning(getLabel('509123', '指标类型不能为空！'));
                return false;
            }
            if (assessDetailData.some(item => {
                let ass = accessItemList.find(accessItem => accessItem.key == item.accessitemid);
                item.itemtype = ass.itemtype;
                if (ass.itemtype == '2' && (item.target == undefined || item.target === '')) {
                    return true;
                } else {
                    return false
                }
            })) {
                message.warning(getLabel('509124', '定量指标需填写目标值！'));
                return false;
            }

            if (100 != assessDetailData.reduce((sum, item) => sum + Number(item.rate), 0).toFixed(2)) {
                message.warning(getLabel('509125', '考核项权重设置不正确,权重总和需等于100！'));
                return false;
            }
            if (assessDetailData.some(i =>
                assessDetailData.some(j => i.id != j.id && i.accessitemid == j.accessitemid && i.description === j.description)
            )) {
                message.warning(getLabel('509126', '相同指标类型的指标描述不能相同！'));
                return false;
            }
        }

        if (accessScoreData.some(item => item.checkuserid == undefined || item.checkuserid === '')) {
            message.warning(getLabel('509119', '评分人不能为空！'));
            return false;
        }
        if (100 != accessScoreData.reduce((sum, item) => sum + Number(item.rate), 0).toFixed(2)) {
            message.warning(getLabel('509120', '评分人权重设置不正确, 权重总和需等于100！'));
            return false;
        }

        let title = '';
        switch (type) {
            case 'save':
                title = getLabel('509127', '确定保存此考核方案？');
                break;
            case 'submit':
                title = getLabel('509128', '确定提交此考核方案？');
                break;
            case 'start':
                title = getLabel('509130', '确定启用此考核方案？');
                break;
            case 'forbidden':
                title = getLabel('509131', '确定禁用此考核方案？');
                break;
        }
        Modal.confirm({
            title: getLabel('386786', '信息确认'),
            content: title,
            onOk: () => {
                operation(type, this.props.updateNum);
            },
        });
    }

    //删除
    doDelete(type) {
        const {operation} = this.store;
        Modal.confirm({
            title: getLabel('386786', '信息确认'),
            content: getLabel('512710', '确定删除此考核方案？'),
            onOk: () => {
                operation(type, this.props.updateNum);
            },
        });
    }

    //获取被考核人上级的上级
    getManagerManager = () => {
        const {form, backSetting, detail: {managerid2, managername2}, auditidsData} = this.store;
        if (managerid2 == '' || managerid2 == '0') {
            message.info(getLabel('509116', '直接上级的上级不存在,请重新选择!'));
        } else {
            if (form.auditids.indexOf(managerid2) == -1) {
                form.auditnames = form.auditnames + "," + managername2;
                form.auditids = form.auditids + "," + managerid2;
            }
        }
    }

    //方案审核操作
    programAudit(operation) {
        let {form: {programid}} = this.store;
        let title = operation == 'approve' ? getLabel('508727', '确定批准此考核方案？') : getLabel('508728', '确定退回此考核方案？');
        let params = {
            programid: programid,
            operation: operation,
        };
        Modal.confirm({
            title: getLabel('386786', '信息确认'),
            content: title,
            onOk: () => {
                this.store.programAuditOperation(params);
            },
        });
    }

    programBrowserUpdateNum() {
        if (typeof this.props.updateNum == "function") {
            this.props.updateNum();
        }
    }
};